# 写入文件
with open('example.txt', 'w', encoding='utf-8') as file:
    file.write('Hello, World!\n')  # 写入一行
    file.write('This is a test file.')  # 写入另一行

# 读取文件
with open('example.txt', 'r', encoding='utf-8') as file:
    content = file.read()  # 读取整个文件内容
    print(content)  # 输出: Hello, World!
                    #       This is a test file.

# 逐行读取文件
with open('example.txt', 'r', encoding='utf-8') as file:
    for line in file:  # 逐行迭代
        print(line.strip())  # 输出: Hello, World!
                             #       This is a test file.